<?php
class UserMenuWidget extends CWidget
{
    public $cssFile,$jsFile,$class;
    protected $items;


    protected static $status = 1;

    protected function registerClientScript()
    {
        if(isset($this->cssFile) || isset($this->jsFile)){
            $cs = Yii::app()->clientScript;
        }
        if(isset($this->cssFile))
        {
            $cs->registerCssFile(Yii::app()->baseUrl.DIRECTORY_SEPARATOR.'css'.DIRECTORY_SEPARATOR.$this->cssFile);
        }
        if(isset($this->jsFile))
        {
            $cs->registerScriptFile(Yii::app()->baseUrl.DIRECTORY_SEPARATOR.'js'.DIRECTORY_SEPARATOR.$this->jsFile);
        }
        if($this->controller->id === 'backend'){
            $cs->registerScript('tb-dropdown',"$('.dropdown-toggle').dropdown();");
        }

    }

    public function init()
    {
        if(!$this->class)
        {
            $this->class = 'nav navbar-nav';
        }
        if($this->cssFile === null)
        {
            $file = 'css'.DIRECTORY_SEPARATOR.'usermenu.css';
            $this->cssFile = Yii::app()->getAssetManager()->publish($file);
        }
        else
        {
            $this->registerClientScript();
        }

        $this->items = $this->getBackendPages();
        parent::init();
    }
    public function getBackendPages()
    {
        $item = array();
        $modules = Yii::app()->getModules();
        if(count($modules))
        {
            foreach($modules as $module=>$options)
            {
                if($module != 'gii')
                {
                    Yii::app()->getModule($module)->basePath = Yii::getPathOfAlias('application.modules.'.$module);//Определяем базовый путь модуля, для предотвращения ошибок Yii::t()
                    $item[] = array('name'=>$module,'label'=>Yii::t(ucfirst($module).'Module.base',$module),'url'=>$module.'/'.'backend/admin');
                }
            }
            return $item;
        }
    }

    /**
     * Поиск ключа в массиве
     *
     * @param $search
     * @param array $items
     * @return bool
     */
    protected function itemSearch($search,$items = array())
    {
        foreach($items as $item)
        {
            if($item['name'] === $search)
            {
                return true;
            }
        }
        return false;
    }

    /**
     *
     */
    protected function getMenuItems()
    {
        $sql = "SELECT id,title,link_path FROM {{menu_item}} WHERE status=:status";
        $q = Yii::app()->db->createCommand($sql);
        $q->bindParam(':status',self::$status,PDO::PARAM_INT);
        $result = $q->query()->readAll();

        return $result;
    }

    public function run()
    {

        if(isset(Yii::app()->user) && Yii::app()->user->isGuest)
        {
            echo CHtml::link(Yii::t('UserModule.base','Registration'),Yii::app()->createUrl('/user/registration'),array('style'=>'float:right;font-size:12px;padding-left:5px'));
            echo CHtml::link(Yii::t('UserModule.base','Login'),Yii::app()->createUrl('/user/login'),array('style'=>'float:right;font-size:12px;'));
        }
        if(!Yii::app()->user->isGuest)
        {
            //if admin
            if(Yii::app()->controller->id === 'backend' && Yii::app()->user->isAdmin)
            {

                echo CHTML::openTag('ul',array('class'=>$this->class));

                echo CHtml::openTag('li');
                echo CHtml::link('<span class="glyphicon glyphicon-home" style="font-size:14px;vertical-align:top;"></span> ',Yii::app()->createUrl('/user/backend/main'),array());
                echo CHtml::closeTag('li');

                $items = $this->items;


                    if($this->itemSearch('menu',$items))
                    {
                        $menu_items = $this->getMenuItems();
                        echo Chtml::openTag('li',array('class'=>'dropdown'));
                        echo Chtml::tag('a',array('href'=>'#','id'=>'dropdownMenu','class'=>'dropdown-toggle','data-toggle'=>'dropdown'),Yii::t('MenuModule.base','menu').CHtml::tag('span',array('class'=>'caret','style'=>'border-top-color:#ffffff;border-bottom-color:#ffffff')));
                        echo Chtml::openTag('ul',array('class'=>'dropdown-menu','role'=>'menu','aria-labelledby'=>'dropdownMenu','style'=>'margin-bottom: 5px;width: 180px;'));
                        echo Chtml::openTag('li');
                        echo CHtml::tag('a',array('href'=>Yii::app()->createUrl('/menu/backend/admin')),Yii::t('MenuModule.base','Manage Menu'));
                        echo Chtml::closeTag('li');

                        echo Chtml::openTag('li',array('class'=>'dropdown-submenu'));
                        echo CHtml::tag('a',array('href'=>Yii::app()->createUrl('path/to/mainmenu')),Yii::t('MenuModule.base',Yii::t('MenuModule.base','Main menu')));
                        echo Chtml::openTag('ul',array('class'=>'dropdown-menu'));
                        echo CHtml::openTag('li');
                        echo CHtml::tag('a',array('tabindex'=>'-1','href'=>Yii::app()->createUrl('/menu/backend/create',array('id'=>1))),Yii::t('MenuModule.base','Add new menu item'));
                        echo Chtml::closeTag('li');
                        echo Chtml::closeTag('ul');
                        echo Chtml::closeTag('li');
                        echo CHtml::tag('li',array('class'=>'divider'));


                        foreach($menu_items as $item)
                        {
                            echo Chtml::openTag('li',array('class'=>'dropdown-submenu'));
                            echo CHtml::tag('a',array('id'=>$item['id'],'href'=>'#','tabindex'=>'-1'),Yii::t('MenuModule.base',$item['title']));
                            echo Chtml::openTag('ul',array('class'=>'dropdown-menu'));
                            echo CHtml::openTag('li');
                            echo CHtml::tag('a',array('tabindex'=>'-2','href'=>Yii::app()->createUrl('/menu/backend/update',array('id'=>$item['id']))),Yii::t('MenuModule.base','Edit menu item'));
                            echo Chtml::closeTag('li');
                            echo Chtml::closeTag('ul');
                            echo Chtml::closeTag('li');
                        }
                        //echo CHtml::tag('a',array('href'=>'#','tabindex'=>'-1'),'More options');



                        echo Chtml::closeTag('ul');
                        echo Chtml::closeTag('li');
                        echo Chtml::closeTag('li');
                    }
                foreach($items as $item)
                {
                    if($item['name'] != 'menu')
                    {
                        echo CHtml::openTag('li');
                        echo CHtml::link($item['label'],Yii::app()->createUrl($item['url']),array());
                    }

                }


                    echo CHtml::closeTag('li');


                echo CHTML::closeTag('ul');
                echo CHTML::openTag('ul',array('class'=>'nav navbar-nav pull-right'));
                echo CHtml::openTag('li');
                echo CHtml::link('<span class="glyphicon glyphicon-off" style="font-size:14px;vertical-align:top;"></span> '.Yii::t('UserModule.base','Logout'),Yii::app()->createUrl('/user/logout'),array());
                echo CHtml::closeTag('li');
                echo CHtml::openTag('li');
                echo CHtml::link('<span class="glyphicon glyphicon-globe" style="font-size:14px;vertical-align:top;"></span> '.Yii::t('UserModule.base','Site'),Yii::app()->createUrl('/site/index'),array());
                echo CHtml::closeTag('li');
                echo CHtml::closeTag('ul');
            }
            if(!Yii::app()->user->isGuest && Yii::app()->controller->id !== 'backend')
            {
                echo CHtml::link('<span class="glyphicon glyphicon-off">'.Yii::t('UserModule.base','Logout'),Yii::app()->createUrl('/user/logout'),array('style'=>'float:right;font-size:12px;padding-left:5px'));
                echo CHtml::link(Yii::t('UserModule.base','Profile'),Yii::app()->createUrl('/user/frontend/profile'),array('style'=>'float:right;font-size:12px;'));
            }
        }

        if(Yii::app()->user->isAdmin)
        {
            if(Yii::app()->controller->id === 'frontend' || Yii::app()->controller->id === 'site')
            {
                echo CHtml::link(Yii::t('UserModule.base','Admin panel'),Yii::app()->createUrl('/user/backend/admin'),array('style'=>'float:right;font-size:12px;padding-right:5px;font-weight:bold;'));
            }


        }
    }
}
